package com.servlet;

import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;

import modle.CardGame;
import modle.CardGames;
import modle.User;
import util.CardPlay;

/**
 * Servlet implementation class ServletDemo
 */
@WebServlet("/GetInitializationDataServlet")
/**
 * 新建牌局，并返回初始牌组给前端
 * @author WXE
 *
 */
public class GetInitializationDataServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GetInitializationDataServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
    @Override 
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		User user = (User) request.getSession().getAttribute("user");
		CardGames cardGames = (CardGames) this.getServletContext().getAttribute("CardGames");
		CardGames.creatCardGame(user);
		int cardGameID = user.getCardGameID();
		Map<Integer, CardGame> games = cardGames.getCardGames();
		CardGame cardGame = games.get(cardGameID);
		Map<String, Object> map = CardPlay.begin(cardGame);
		
		response.getWriter().println(JSON.toJSONString(map));
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
    @Override 
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
